package system

import (
	"gin-admin/common/dto/response"
	"gin-admin/core"
	serviceRequest "gin-admin/service/dto/request"
	"gin-admin/service/v2/system"

	"github.com/gin-gonic/gin"
)

type MenuHandler struct {
	MenuService system.MenuService
}

func NewMenuHandlerApp(service *system.MenuService) *MenuHandler {
	return &MenuHandler{
		MenuService: *service,
	}
}

// @Summary 创建菜单信息(v2)
// @Tags /v2/菜单管理
// @Accept json
// @Produce json
// @Param data body request.CreateMenuRequest true "创建菜单信息请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/menu/create [post]
func (mh *MenuHandler) CreateMenuInfoHandler(ctx *gin.Context) {
	var crateRequest serviceRequest.CreateMenuRequest
	if !core.ValidateParams(ctx, &crateRequest) {
		return
	}
	if err := mh.MenuService.CreateMenuInfoService(&crateRequest); err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.SuccessNoData(ctx)
}

// @Summary 删除菜单信息(v2)
// @Tags /v2/菜单管理
// @Accept json
// @Produce json
// @Param id path string true "删除菜单信息请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/menu/delete/{id} [delete]
func (mh *MenuHandler) DeleteMenuInfoHandler(ctx *gin.Context) {
	id := ctx.Param("id")
	if !core.ValidateParams(ctx, &id) {
		return
	}
	responseData, err := mh.MenuService.DeleteMenuInfoService(id)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 修改菜单信息(v2)
// @Tags /v2/菜单管理
// @Accept json
// @Produce json
// @Param data body request.ModifyMenuRequest true "修改菜单信息请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/menu/modify [put]
func (mh *MenuHandler) ModifyMenuInfoHandler(ctx *gin.Context) {
	var modifyRequest serviceRequest.ModifyMenuRequest
	if !core.ValidateParams(ctx, &modifyRequest) {
		return
	}
	responseData, err := mh.MenuService.ModifyMenuInfoService(&modifyRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 获取菜单详情(v2)
// @Tags /v2/菜单管理
// @Accept json
// @Produce json
// @Param id path string true "获取菜单详情请求参数"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/menu/info/{id} [get]
func (mh *MenuHandler) GetMenuInfoHandler(ctx *gin.Context) {
	id := ctx.Param("id")
	if !core.ValidateParams(ctx, &id) {
		return
	}
	responseData, err := mh.MenuService.GetMenuInfoService(id)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 获取菜单列表(v2)
// @Tags /v2/菜单管理
// @Accept json
// @Produce json
// @Param name query string true "菜单名称"
// @Param code query string true "菜单代码"
// @Param status query int true "菜单状态"
// @Param level query string true "菜单级别"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/menu/list [get]
func (mh *MenuHandler) GetMenuListHandler(ctx *gin.Context) {
	var listRequest serviceRequest.MenuQueryRequest
	if !core.ValidateParams(ctx, &listRequest) {
		return
	}
	responseData, err := mh.MenuService.GetMenuListService(&listRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}

// @Summary 获取菜单分页列表(v2)
// @Tags /v2/菜单管理
// @Accept json
// @Produce json
// @Param page query int true "页码"
// @Param size query int true "每页大小"
// @Param name query string true "菜单名称"
// @Param code query string true "菜单代码"
// @Param status query int true "菜单状态"
// @Param level query string true "菜单级别"
// @Success 200 {object} response.Response "成功"
// @Failure 400 {object} response.Response "失败"
// @Router /api/v2/menu/page [get]
func (mh *MenuHandler) GetMenuPageHandler(ctx *gin.Context) {
	var listRequest serviceRequest.MenuQueryRequest
	if !core.ValidateParams(ctx, &listRequest) {
		return
	}
	responseData, err := mh.MenuService.GetMenuPageService(&listRequest)
	if err != nil {
		response.FailedWithCode(ctx, err.Code)
		return
	}
	response.Success(ctx, responseData)
}
